<template>
  <div class="product-management">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">产品管理</h1>
        <p class="page-description">管理商品信息和库存</p>
      </div>
      <div class="header-actions">
        <button class="btn btn-success" @click="showAddModal">
          <i class="fas fa-plus"></i>
          添加产品
        </button>
        <button class="btn btn-primary" @click="refreshProducts">
          <i class="fas fa-refresh"></i>
          刷新
        </button>
      </div>
    </div>
    
    <!-- 搜索和过滤 -->
    <div class="filters-section">
      <div class="search-box">
        <div class="search-input-wrapper">
          <i class="fas fa-search search-icon"></i>
          <input
            v-model="searchQuery"
            type="text"
            placeholder="搜索产品名称或规格..."
            class="search-input"
          />
          <button
            v-if="searchQuery"
            class="clear-search"
            @click="clearSearch"
          >
            <i class="fas fa-times"></i>
          </button>
        </div>
      </div>
      
      <div class="filter-controls">
        <select v-model="categoryFilter" @change="applyFilters" class="filter-select">
          <option value="">全部分类</option>
          <option
            v-for="category in categories" 
            :key="category.categoryId" 
            :value="category.categoryId"
          >
            {{ category.categoryName }}
          </option>
        </select>
      </div>
    </div>

    <!-- 悬浮批量操作按钮 -->
    <div v-if="selectedProducts.length > 0" class="floating-batch-actions">
      <div class="batch-main">
      <div class="batch-info">
          <span class="selected-count">{{ selectedProducts.length }}</span>
          <span class="selected-text">已选择</span>
      </div>
      <div class="batch-buttons">
          <button @click="showBatchMenu = !showBatchMenu" class="batch-toggle-btn" :class="{ active: showBatchMenu }">
            <i class="fas" :class="showBatchMenu ? 'fa-times' : 'fa-cog'"></i>
          </button>
      </div>
    </div>

      <!-- 批量操作菜单 -->
      <div v-if="showBatchMenu" class="batch-menu">
        <div class="batch-menu-header">
          <h4>批量操作</h4>
        </div>
        <div class="batch-menu-content">
          <div class="batch-action-group">
            <label class="action-label">危险操作</label>
            <div class="action-row">
              <button @click="batchDeleteProducts" class="btn btn-danger btn-sm btn-full">
                <i class="fas fa-trash"></i>
                批量删除产品
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 产品列表 -->
    <div class="products-section">
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <span>加载产品数据中...</span>
      </div>
      
      <div v-else-if="filteredProducts.length === 0" class="empty-state">
        <i class="fas fa-box"></i>
        <h3>{{ searchQuery || categoryFilter ? '未找到匹配的产品' : '暂无产品数据' }}</h3>
        <p>{{ searchQuery || categoryFilter ? '请尝试调整搜索条件' : '点击添加产品按钮创建第一个产品' }}</p>
    </div>

      <div v-else class="products-table-container">
        <table class="products-table">
          <thead>
            <tr>
              <th class="checkbox-column">
                <input
                  type="checkbox"
                  :checked="isAllSelected"
                  @change="toggleSelectAll"
                  class="batch-checkbox"
                />
              </th>
              <th>产品信息</th>
              <th>分类</th>
              <th>价格</th>
              <th>库存</th>
              <th>规格</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr
              v-for="product in paginatedProducts"
              :key="product.productId"
              class="product-row"
            >
              <td class="checkbox-column">
                <input
                  type="checkbox"
                  :value="product.productId"
                  v-model="selectedProducts"
                  class="batch-checkbox"
                />
              </td>
              
              <td class="product-info-cell">
            <div class="product-image">
                  <img
                    :src="product.productImage || '/ceholder-product.png'"
                    :alt="product.productName"
                    @error="handleImageError"
                  />
            </div>
                <div class="product-details">
                  <div class="product-name">{{ product.productName }}</div>
                  <div class="product-id">ID: {{ product.productId }}</div>
                </div>
              </td>
              
              <td class="category-cell">
                {{ getCategoryName(product.categoryId) }}
              </td>
              
              <td class="price-cell">
                <div class="price-value">¥{{ formatMoney(product.price) }}</div>
              </td>
              
              <td class="stock-cell">
                <div class="stock-info">
                  <span class="stock-number" :class="getStockClass(product.stock)">
                    {{ product.stock }}
                  </span>
                  <span class="stock-status" :class="getStockClass(product.stock)">
                    {{ getStockStatus(product.stock) }}
                  </span>
                </div>
              </td>
              
              <td class="spec-cell">
                <div class="spec-text">
                  {{ product.specParams || '-' }}
                </div>
              </td>
              
              <td class="status-cell">
                <span
                  class="status-badge"
                  :class="product.stock > 0 ? 'status-available' : 'status-unavailable'"
                >
                  {{ product.stock > 0 ? '有库存' : '缺货' }}
                </span>
              </td>
              
              <td class="actions-cell">
                <div class="action-buttons">
                  <button
                    class="btn btn-sm btn-info"
                    @click="editProduct(product)"
                    title="编辑产品"
                  >
                    <i class="fas fa-edit"></i>
                  </button>
                  <button
                    class="btn btn-sm btn-danger"
                    @click="confirmDeleteProduct(product)"
                    title="删除产品"
                    :disabled="deletingProductId === product.productId"
                  >
                    <i
                      class="fas"
                      :class="deletingProductId === product.productId ? 'fa-spinner fa-spin' : 'fa-trash'"
                    ></i>
                  </button>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      
      <!-- 分页 -->
      <div v-if="totalPages > 1" class="pagination-container">
        <div class="pagination-info">
          显示 {{ (currentPage - 1) * pageSize + 1 }} - {{ Math.min(currentPage * pageSize, filteredProducts.length) }} 
          共 {{ filteredProducts.length }} 条记录
        </div>
        <div class="pagination-controls">
          <button
            class="btn btn-sm btn-secondary"
            @click="goToPage(currentPage - 1)"
            :disabled="currentPage === 1"
          >
            <i class="fas fa-chevron-left"></i>
          </button>
          
          <div class="page-numbers">
            <button
              v-for="page in visiblePages"
              :key="page"
              class="btn btn-sm"
              :class="page === currentPage ? 'btn-primary' : 'btn-secondary'"
              @click="goToPage(page)"
            >
              {{ page }}
            </button>
          </div>
          
          <button
            class="btn btn-sm btn-secondary"
            @click="goToPage(currentPage + 1)"
            :disabled="currentPage === totalPages"
          >
            <i class="fas fa-chevron-right"></i>
          </button>
        </div>
      </div>
    </div>

    <!-- 添加/编辑产品模态框 -->
    <div v-if="showProductModal" class="modal-overlay" @click="closeProductModal">
      <div class="modal-content large-modal" @click.stop>
        <div class="modal-header">
          <h3>{{ isEditing ? '编辑产品' : '添加产品' }}</h3>
          <button class="modal-close" @click="closeProductModal">
            <i class="fas fa-times"></i>
          </button>
        </div>
        
        <form @submit.prevent="handleSubmit" class="modal-body">
          <div class="form-grid">
            <div class="form-group">
              <label class="form-label">产品名称 <span class="required">*</span></label>
              <input
                v-model="productForm.productName"
                type="text"
                class="form-input"
                :class="{ 'error': errors.productName }"
                placeholder="请输入产品名称"
                @blur="validateProductName"
              />
              <span v-if="errors.productName" class="error-text">{{ errors.productName }}</span>
            </div>
            
            <div class="form-group">
              <label class="form-label">分类 <span class="required">*</span></label>
              <select
                v-model="productForm.categoryId"
                class="form-select"
                :class="{ 'error': errors.categoryId }"
                @change="validateCategoryId"
              >
                <option value="">请选择分类</option>
                <option
              v-for="category in categories" 
              :key="category.categoryId" 
              :value="category.categoryId"
                >
                  {{ category.categoryName }}
                </option>
              </select>
              <span v-if="errors.categoryId" class="error-text">{{ errors.categoryId }}</span>
            </div>
            
            <div class="form-group">
              <label class="form-label">价格 <span class="required">*</span></label>
              <input
            v-model="productForm.price" 
                type="number"
                step="0.01"
                min="0"
                class="form-input"
                :class="{ 'error': errors.price }"
                placeholder="0.00"
                @blur="validatePrice"
              />
              <span v-if="errors.price" class="error-text">{{ errors.price }}</span>
            </div>
            
            <div class="form-group">
              <label class="form-label">库存 <span class="required">*</span></label>
              <input
            v-model="productForm.stock" 
                type="number"
                min="0"
                class="form-input"
                :class="{ 'error': errors.stock }"
                placeholder="0"
                @blur="validateStock"
              />
              <span v-if="errors.stock" class="error-text">{{ errors.stock }}</span>
            </div>
          </div>
          
          <div class="form-group">
            <label class="form-label">规格参数</label>
            <input
            v-model="productForm.specParams" 
              type="text"
              class="form-input"
              placeholder="请输入产品规格参数（可选）"
            />
      </div>
          
          <div class="form-group">
            <label class="form-label">产品图片URL</label>
            <input
              v-model="productForm.productImage"
              type="url"
              class="form-input"
              placeholder="请输入产品图片URL（可选）"
            />
          </div>
          
          <div class="form-group">
            <label class="form-label">产品描述</label>
            <textarea
              v-model="productForm.description"
              class="form-textarea"
              placeholder="请输入产品描述（可选）"
              rows="4"
            ></textarea>
          </div>
        </form>
        
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary"
            @click="closeProductModal"
          >
            取消
          </button>
          <button
            type="button"
            class="btn btn-primary"
            @click="handleSubmit"
            :disabled="submitting || !isFormValid"
          >
            <i v-if="submitting" class="fas fa-spinner fa-spin"></i>
            {{ submitting ? '保存中...' : (isEditing ? '更新' : '添加') }}
          </button>
          </div>
      </div>
    </div>
    
    <!-- 删除确认模态框 -->
    <div v-if="showDeleteModal" class="modal-overlay" @click="showDeleteModal = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>确认删除</h3>
          <button class="modal-close" @click="showDeleteModal = false">
            <i class="fas fa-times"></i>
          </button>
        </div>
        
        <div class="modal-body">
          <p>您确定要删除产品 <strong>"{{ productToDelete?.productName }}"</strong> 吗？</p>
          <p class="warning-text">此操作不可撤销，请谨慎操作。</p>
        </div>
        
        <div class="modal-footer">
          <button
            class="btn btn-secondary"
            @click="showDeleteModal = false"
          >
            取消
          </button>
          <button
            class="btn btn-danger"
            @click="deleteProduct"
            :disabled="deletingProductId === productToDelete?.productId"
          >
            <i
              class="fas"
              :class="deletingProductId === productToDelete?.productId ? 'fa-spinner fa-spin' : 'fa-trash'"
            ></i>
            确认删除
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, reactive, onMounted, watch } from 'vue'
import { productAPI, categoryAPI } from '@/api'

export default {
  name: 'ProductManagement',
  setup() {
    // 响应式数据
    const products = ref([])
    const categories = ref([])
    const loading = ref(false)
    const searchQuery = ref('')
    const categoryFilter = ref('')
    const currentPage = ref(1)
    const pageSize = ref(10)
    const showProductModal = ref(false)
    const showDeleteModal = ref(false)
    const isEditing = ref(false)
    const submitting = ref(false)
    const deletingProductId = ref(null)
    const productToDelete = ref(null)
    const selectedProducts = ref([])
    const isAllSelected = ref(false)
    const showBatchMenu = ref(false)

    const productForm = reactive({
      productId: null,
      productName: '',
      categoryId: '',
      price: '',
      stock: '',
      specParams: '',
      productImage: '',
      description: ''
    })
    
    const errors = reactive({
      productName: '',
      categoryId: '',
      price: '',
      stock: ''
    })
    
    // 计算属性
    const filteredProducts = computed(() => {
      let filtered = products.value

      // 搜索过滤
      if (searchQuery.value) {
        const query = searchQuery.value.toLowerCase()
        filtered = filtered.filter(product => 
          product.productName?.toLowerCase().includes(query) ||
          product.specParams?.toLowerCase().includes(query)
        )
      }
      
      // 分类过滤
      if (categoryFilter.value) {
        filtered = filtered.filter(product => product.categoryId === parseInt(categoryFilter.value))
      }

      return filtered
    })

    const totalPages = computed(() => {
      return Math.ceil(filteredProducts.value.length / pageSize.value)
    })
    
    const paginatedProducts = computed(() => {
      const start = (currentPage.value - 1) * pageSize.value
      const end = start + pageSize.value
      return filteredProducts.value.slice(start, end)
    })
    
    const visiblePages = computed(() => {
      const pages = []
      const maxVisible = 5
      let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
      let end = Math.min(totalPages.value, start + maxVisible - 1)
      
      if (end - start + 1 < maxVisible) {
        start = Math.max(1, end - maxVisible + 1)
      }
      
      for (let i = start; i <= end; i++) {
        pages.push(i)
      }
      
      return pages
    })
    
    const isFormValid = computed(() => {
      return productForm.productName.trim() && 
             productForm.categoryId && 
             productForm.price && 
             productForm.stock !== '' &&
             !errors.productName && 
             !errors.categoryId && 
             !errors.price && 
             !errors.stock
    })
    
    // 方法
    const loadProducts = async () => {
      loading.value = true
      try {
        const response = await productAPI.getAllProducts()
        if (response.success) {
          products.value = response.products
        } else {
          console.error('加载产品列表失败:', response.message)
        }
      } catch (error) {
        console.error('加载产品列表错误:', error)
      } finally {
        loading.value = false
      }
    }

    const loadCategories = async () => {
      try {
        const response = await categoryAPI.getAllCategories()
        if (response.success) {
          categories.value = response.categories
        }
      } catch (error) {
        console.error('加载分类列表错误:', error)
      }
    }
    
    const refreshProducts = () => {
      loadProducts()
    }
    
    const clearSearch = () => {
      searchQuery.value = ''
      currentPage.value = 1
    }
    
    const applyFilters = () => {
      currentPage.value = 1
    }
    
    const goToPage = (page) => {
      if (page >= 1 && page <= totalPages.value) {
        currentPage.value = page
      }
    }
    
    const getCategoryName = (categoryId) => {
      const category = categories.value.find(c => c.categoryId === categoryId)
      return category ? category.categoryName : '未知分类'
    }
    
    const formatMoney = (amount) => {
      if (!amount) return '0.00'
      return parseFloat(amount).toLocaleString('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      })
    }
    
    const getStockClass = (stock) => {
      if (stock <= 0) return 'stock-out'
      if (stock <= 10) return 'stock-low'
      return 'stock-normal'
    }
    
    const getStockStatus = (stock) => {
      if (stock <= 0) return '缺货'
      if (stock <= 10) return '低库存'
      return '正常'
    }
    
    const handleImageError = (event) => {
      event.target.src = '/placeholder-product.png'
    }
    
    const showAddModal = () => {
      resetForm()
      isEditing.value = false
      showProductModal.value = true
    }
    
    const editProduct = (product) => {
      productForm.productId = product.productId
      productForm.productName = product.productName
      productForm.categoryId = product.categoryId
      productForm.price = product.price
      productForm.stock = product.stock
      productForm.specParams = product.specParams || ''
      productForm.productImage = product.productImage || ''
      productForm.description = product.description || ''
      
      isEditing.value = true
      showProductModal.value = true
    }
    
    const closeProductModal = () => {
      showProductModal.value = false
      resetForm()
    }
    
    const resetForm = () => {
      productForm.productId = null
      productForm.productName = ''
      productForm.categoryId = ''
      productForm.price = ''
      productForm.stock = ''
      productForm.specParams = ''
      productForm.productImage = ''
      productForm.description = ''
      
      Object.keys(errors).forEach(key => {
        errors[key] = ''
      })
    }
    
    const validateProductName = () => {
      if (!productForm.productName.trim()) {
        errors.productName = '请输入产品名称'
      } else if (productForm.productName.trim().length < 2) {
        errors.productName = '产品名称至少2个字符'
      } else {
        errors.productName = ''
      }
    }
    
    const validateCategoryId = () => {
      if (!productForm.categoryId) {
        errors.categoryId = '请选择分类'
      } else {
        errors.categoryId = ''
      }
    }
    
    const validatePrice = () => {
      if (!productForm.price) {
        errors.price = '请输入价格'
      } else if (parseFloat(productForm.price) <= 0) {
        errors.price = '价格必须大于0'
      } else {
        errors.price = ''
      }
    }
    
    const validateStock = () => {
      if (productForm.stock === '') {
        errors.stock = '请输入库存'
      } else if (parseInt(productForm.stock) < 0) {
        errors.stock = '库存不能小于0'
      } else {
        errors.stock = ''
      }
    }
    
    const handleSubmit = async () => {
      // 验证所有字段
      validateProductName()
      validateCategoryId()
      validatePrice()
      validateStock()
      
      if (!isFormValid.value) return
      
      submitting.value = true
      
      try {
        const data = {
          productName: productForm.productName.trim(),
          categoryId: parseInt(productForm.categoryId),
          price: parseFloat(productForm.price),
          stock: parseInt(productForm.stock),
          specParams: productForm.specParams.trim() || null,
          productImage: productForm.productImage.trim() || null,
          description: productForm.description.trim() || null
        }
        
        let response
        if (isEditing.value) {
          response = await productAPI.updateProduct(productForm.productId, data)
        } else {
          response = await productAPI.addProduct(data)
        }
        
        if (response.success) {
          closeProductModal()
          await loadProducts()
        } else {
          alert(response.message || (isEditing.value ? '更新产品失败' : '添加产品失败'))
        }
      } catch (error) {
        console.error('提交产品数据错误:', error)
        alert(isEditing.value ? '更新产品失败，请检查网络连接' : '添加产品失败，请检查网络连接')
      } finally {
        submitting.value = false
      }
    }
    
    const confirmDeleteProduct = (product) => {
      productToDelete.value = product
      showDeleteModal.value = true
    }
    
    const deleteProduct = async () => {
      if (!productToDelete.value) return
      
      deletingProductId.value = productToDelete.value.productId
      
      try {
        const response = await productAPI.deleteProduct(productToDelete.value.productId)
        if (response.success) {
          showDeleteModal.value = false
          await loadProducts()
        } else {
          alert(response.message || '删除产品失败')
        }
      } catch (error) {
        console.error('删除产品错误:', error)
        alert('删除产品失败，请检查网络连接')
      } finally {
        deletingProductId.value = null
        productToDelete.value = null
      }
    }
    
    const toggleSelectAll = () => {
      isAllSelected.value = !isAllSelected.value
      selectedProducts.value = isAllSelected.value ? paginatedProducts.value.map(p => p.productId) : []
    }
    
    const batchDeleteProducts = async () => {
      if (selectedProducts.value.length === 0) return
      
      if (confirm(`确定要删除选中的 ${selectedProducts.value.length} 个产品吗？`)) {
        try {
          const response = await productAPI.deleteProducts(selectedProducts.value)
          if (response.success) {
            selectedProducts.value = []
            await loadProducts()
          } else {
            alert(response.message || '批量删除产品失败')
          }
      } catch (error) {
          console.error('批量删除产品错误:', error)
          alert('批量删除产品失败，请检查网络连接')
        }
      }
    }

    // 监听搜索和过滤变化，重置页码
    watch([searchQuery, categoryFilter], () => {
      currentPage.value = 1
    })
    
    // 点击外部关闭批量操作菜单
    const handleClickOutside = (event) => {
      if (!event.target.closest('.floating-batch-actions')) {
        showBatchMenu.value = false
      }
    }

    // 生命周期
    onMounted(() => {
      loadProducts()
      loadCategories()
      document.addEventListener('click', handleClickOutside)
    })

    return {
      products,
      categories,
      loading,
      searchQuery,
      categoryFilter,
      currentPage,
      pageSize,
      showProductModal,
      showDeleteModal,
      isEditing,
      submitting,
      deletingProductId,
      productToDelete,
      productForm,
      errors,
      filteredProducts,
      totalPages,
      paginatedProducts,
      visiblePages,
      isFormValid,
      loadProducts,
      refreshProducts,
      clearSearch,
      applyFilters,
      goToPage,
      getCategoryName,
      formatMoney,
      getStockClass,
      getStockStatus,
      handleImageError,
      showAddModal,
      editProduct,
      closeProductModal,
      validateProductName,
      validateCategoryId,
      validatePrice,
      validateStock,
      handleSubmit,
      confirmDeleteProduct,
      deleteProduct,
      selectedProducts,
      isAllSelected,
      toggleSelectAll,
      batchDeleteProducts,
      showBatchMenu
    }
  }
}
</script>

<style scoped>
.product-management {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 页面头部 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  background: white;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.header-content h1 {
  margin: 0 0 8px 0;
  color: #2c3e50;
  font-size: 24px;
  font-weight: 600;
}

.page-description {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

/* 搜索和过滤 */
.filters-section {
  display: flex;
  gap: 16px;
  align-items: center;
  background: white;
  padding: 20px 24px;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.search-box {
  flex: 1;
}

.search-input-wrapper {
  position: relative;
  max-width: 400px;
}

.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #7f8c8d;
  font-size: 14px;
}

.search-input {
  width: 100%;
  padding: 12px 16px 12px 44px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.search-input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.clear-search {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #7f8c8d;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.3s ease;
}

.clear-search:hover {
  color: #e74c3c;
}

.filter-controls {
  display: flex;
  gap: 12px;
  align-items: center;
}

.filter-select {
  padding: 10px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 14px;
  background: white;
  cursor: pointer;
  transition: border-color 0.3s ease;
}

.filter-select:focus {
  outline: none;
  border-color: #3498db;
}

/* 悬浮批量操作按钮 */
.floating-batch-actions {
  position: fixed;
  bottom: 30px;
  right: 30px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  border: 1px solid #e9ecef;
  overflow: hidden;
  transition: all 0.3s ease;
}

.batch-main {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
}

.batch-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.selected-count {
  font-size: 18px;
  font-weight: 700;
  color: #3498db;
  background: #ecf4fd;
  padding: 4px 8px;
  border-radius: 8px;
  min-width: 28px;
  text-align: center;
}

.selected-text {
  font-size: 14px;
  color: #7f8c8d;
  font-weight: 500;
}

.batch-buttons {
  position: relative;
}

.batch-toggle-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #3498db;
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.3s ease;
}

.batch-toggle-btn:hover {
  background: #2980b9;
  transform: scale(1.05);
}

.batch-toggle-btn.active {
  background: #e74c3c;
  transform: rotate(90deg);
}

.batch-menu {
  border-top: 1px solid #f1f3f4;
  background: #fafbfc;
}

.batch-menu-header {
  padding: 12px 20px;
  border-bottom: 1px solid #f1f3f4;
  background: #ffffff;
}

.batch-menu-header h4 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: #2c3e50;
}

.batch-menu-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px 20px;
}

.batch-action-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-label {
  font-size: 12px;
  font-weight: 600;
  color: #7f8c8d;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.action-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-full {
  width: 100%;
  justify-content: center;
}

/* 产品列表 */
.products-section {
  background: white;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #7f8c8d;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #ecf0f1;
  border-top: 3px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #7f8c8d;
}

.empty-state i {
  font-size: 64px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state h3 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

/* 表格样式 */
.products-table-container {
  overflow-x: auto;
}

.products-table {
  width: 100%;
  border-collapse: collapse;
}

.products-table th {
  background: #f8f9fa;
  padding: 16px 20px;
  text-align: left;
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
  border-bottom: 1px solid #e1e5e9;
  white-space: nowrap;
}

.products-table td {
  padding: 16px 20px;
  border-bottom: 1px solid #f1f3f4;
  vertical-align: middle;
}

.product-row:hover {
  background: #f8f9fa;
}

.product-info-cell {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 200px;
}

.product-image {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  overflow: hidden;
  background: #ecf0f1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.product-name {
  font-weight: 500;
  color: #2c3e50;
  font-size: 14px;
  line-height: 1.3;
}

.product-id {
  font-size: 12px;
  color: #7f8c8d;
}

.category-cell {
  font-size: 13px;
  color: #5a6c7d;
}

.price-cell {
  min-width: 100px;
}

.price-value {
  font-weight: 600;
  color: #27ae60;
  font-size: 14px;
}

.stock-cell {
  min-width: 100px;
}

.stock-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stock-number {
  font-weight: 600;
  font-size: 14px;
}

.stock-status {
  font-size: 12px;
}

.stock-normal {
  color: #27ae60;
}

.stock-low {
  color: #f39c12;
}

.stock-out {
  color: #e74c3c;
}

.spec-cell {
  max-width: 150px;
}

.spec-text {
  font-size: 13px;
  color: #5a6c7d;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.status-available {
  background: #d4edda;
  color: #155724;
}

.status-unavailable {
  background: #f8d7da;
  color: #721c24;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

/* 分页 */
.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-top: 1px solid #e1e5e9;
}

.pagination-info {
  font-size: 14px;
  color: #7f8c8d;
}

.pagination-controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

.page-numbers {
  display: flex;
  gap: 4px;
}

/* 按钮样式 */
.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: #3498db;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: #2980b9;
}

.btn-secondary {
  background: #ecf0f1;
  color: #7f8c8d;
}

.btn-secondary:hover:not(:disabled) {
  background: #d5dbdb;
}

.btn-success {
  background: #28a745;
  color: white;
}

.btn-success:hover:not(:disabled) {
  background: #218838;
}

.btn-info {
  background: #17a2b8;
  color: white;
}

.btn-info:hover:not(:disabled) {
  background: #138496;
}

.btn-danger {
  background: #dc3545;
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: #c82333;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(5px);
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 500px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}

.large-modal {
  width: 700px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px;
  border-bottom: 1px solid #e1e5e9;
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #7f8c8d;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.3s ease;
}

.modal-close:hover {
  color: #e74c3c;
}

.modal-body {
  padding: 24px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #2c3e50;
  font-size: 14px;
}

.required {
  color: #e74c3c;
}

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  font-family: inherit;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.form-input.error,
.form-select.error {
  border-color: #e74c3c;
}

.form-textarea {
  resize: vertical;
  min-height: 100px;
}

.error-text {
  color: #e74c3c;
  font-size: 12px;
  margin-top: 4px;
  display: block;
}

.warning-text {
  color: #e67e22;
  font-size: 14px;
  margin: 8px 0 0 0;
}

.modal-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 24px;
  border-top: 1px solid #e1e5e9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .filters-section {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
  
  .search-input-wrapper {
    max-width: none;
  }
  
  .products-table {
    font-size: 12px;
  }
  
  .products-table th,
  .products-table td {
    padding: 12px 8px;
  }
  
  .product-info-cell {
    min-width: 150px;
  }
  
  .spec-cell {
    max-width: 100px;
  }
  
  .pagination-container {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .pagination-controls {
    justify-content: center;
  }
  
  .modal-content {
    width: 95vw;
    margin: 20px;
  }
  
  .form-grid {
    grid-template-columns: 1fr;
  }
}
</style> 